library(estimatr) 
library(ggplot2)
library(dplyr)
library(dotwhisker)
library(gridExtra)
library(coefplot)
library(tidyverse)


# Figure 8: Selecting Nuclear Retaliation versus Supporting Nuclear Retaliation (U.S. Sample)

setwd("/Users/dmin/Dropbox/Replication_Data/Paper_1/")

COMdat <- read.csv("Paper_1_Data_Clean_tidy.csv", stringsAsFactors = FALSE)




gg_df<-
  COMdat %>% 
  group_by(Nationality) %>% 
  filter(Nationality == 1) %>%
  summarize(tidy(lm_robust(cbind(NuclearMissile, SupNucRetal) ~ Nuclear + Japan +Tripwire +Retaliation+Trump, data = cur_data()
  )
  ))

gg_df <-
  gg_df %>%
  filter(term != "(Intercept)") %>%
  mutate(term_label =
           factor(
             term,
             levels = c("Trump",
                        "Nuclear",
                        "Tripwire",
                        "Retaliation",
                        "Japan"
                        ),
             labels = c("Trump Supporter",
               "DPRK Attack Uses\nNuclear Weapons",
               "US Military Casualties",
               "DPRK Threat of Retaliation",
               "Target is Japan"
             )
           ),
         outcome_label = factor(outcome, levels = c("NuclearMissile","SupNucRetal"),
                                labels = c("Selected Nuclear Use", "Supported Nuclear Use"))
  )

label_df <- 
  gg_df %>% 
  filter(term_label == "Trump Supporter")



g <-
  ggplot(data = gg_df,
         aes(estimate, term_label, color = outcome_label, group = outcome_label, shape = outcome_label)) +
  geom_point(position = position_dodgev(height = 0.5)) +
  geom_linerange(aes(xmin = conf.low, xmax = conf.high), position = position_dodgev(height = 0.5)) +
  geom_text(data = label_df, aes(label = outcome_label, x = conf.low - 0.03), position = position_dodgev(height = 0.5)) +
  geom_vline(xintercept=0, linetype="dotted")+
  theme_bw() +
  theme(
    plot.title = element_text(face = "bold", size = 12),
    legend.position = "none",
    # legend.background = element_rect(colour="grey80"),
    axis.title.y = element_blank()
  ) +
  scale_color_manual(values = c("indianred2","steelblue2")) +
  labs(x = "Average Treatment Effect Estimate") +
  scale_y_discrete(limits=rev)+
  coord_cartesian(xlim=c(-0.05,.25))
g


ggsave("Image_8.png", plot=g, width=3500, height=1900, units=c("px"))